import { defineStore } from "pinia";
import { computed, ref } from "vue";
import axios from "axios";

export const useCountStore = defineStore("counter", () => {
  // 定义数据
  const count = ref(0);
  const list = ref<any>([]);
  const URL = "http://geek.itheima.net/v1_0/channels";

  //定义修改数据的方法，同步，异步都可以
  const increment = () => {
    count.value++;
  };

  //异步action
  const loadList = async () => {
    const res = await axios.get(URL);
    list.value = res.data.data.channels;
  };

  //getter定义
  const doubleCount = computed(() => count.value * 2);

  //返回
  return {
    count,
    doubleCount,
    increment,
    list,
    loadList,
  };
});
